﻿/*
 * Cratos Tools - General
 * Cratos Error class
 * 
 * Copyright (c) 2011-2012 Draco
 * 
 * This file is part of Cratos.
 * 
 * Cratos is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * Cratos is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Cratos.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *       .==.        .==.
 *      //'^\\      //^'\\
 *     // ^ ^\(\__/)/^ ^^\\
 *    //^ ^^ ^/6  6\ ^^ ^ \\
 *   //^ ^^ ^/( .. )\^ ^ ^ \\
 *  // ^^ ^/\| V""V|/\^ ^ ^ \\
 * //^^/\/ /  '~~'  \ \/\^ ^ \\
 *  ------------------------------------
 *  HERE BE DRAGONS
 *  
 *  Johan Dercksen - johan@draco.co.za
 */
using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;

namespace Cratos.Tools.General
{
	/// <summary>
	/// Cratos Error class
	/// </summary>
	[Serializable]
	public class CratosError
	{
		#region Class Constant declarations
		#endregion

		#region Class Event declarations
		#endregion

		#region Class Variable declarations
		#endregion

		#region Class Constructors & Destructors
		/// <summary>
		/// Class Constructor
		/// </summary>
		/// <param name="errorSource">Error Source</param>
		/// <param name="errorMessage">Error Message</param>
		/// <param name="logSeverity">Log Severity</param>
		public CratosError(string errorSource, CratosLogSeverity logSeverity, string errorMessage)
		{
			// Save the incoming values
			this.Date							= DateTime.Now;
			this.Source						= errorSource;
			this.Severity					= logSeverity;
			this.Message					= errorMessage;
			this.RuntimeException	= null;
		}

		/// <summary>
		/// Class Constructor
		/// </summary>
		/// <param name="errorSource">Error Source</param>
		/// <param name="errorMessage">Error Message</param>
		/// <param name="logSeverity">Log Severity</param>
		/// <param name="runtimeException">Runtime Exception</param>
		public CratosError(string errorSource, CratosLogSeverity logSeverity, string errorMessage, Exception runtimeException)
		{
			// Save the incoming values
			this.Date							= DateTime.Now;
			this.Source						= errorSource;
			this.Severity					= logSeverity;
			this.Message					= errorMessage;
			this.RuntimeException = runtimeException;
		}
		#endregion

		#region Class Properties
		/// <summary>
		/// Error Date
		/// </summary>
		public DateTime	Date { get; set; }

		/// <summary>
		/// Error Source
		/// </summary>
		public string Source { get; set; }

		/// <summary>
		/// Error Severity
		/// </summary>
		public CratosLogSeverity Severity { get; set; }

		/// <summary>
		/// Error Message
		/// </summary>
		public string	Message { get; set; }

		/// <summary>
		/// Runtime Exception details
		/// </summary>
		public Exception RuntimeException { get; set; }
		#endregion

		#region Class Methods
		/// <summary>
		/// To String method
		/// </summary>
		/// <returns>A string respresentation of the error</returns>
		public override string ToString()
		{
			// Return the relevant data
			if (string.IsNullOrEmpty(this.Message)) { return ("Unknown error"); }
			else
			{
				if (this.RuntimeException == null)
				{
					return (string.Format("[{0} - {1}] {2} - {3}",
																this.Date.ToString("dd/MM/yyyy HH:mm:ss"),
																this.Severity, this.Source, this.Message));
				}
				else
				{
					return (string.Format("[{0} - {1}] {2} - {3}\n{4}",
																this.Date.ToString("dd/MM/yyyy HH:mm:ss"),
																this.Severity, this.Source, this.Message,
																this.RuntimeException.ToString()));
				}
			}
		}
		#endregion

		#region Class Support Methods
		#endregion
	}
}
